package com.hu.zhcc.shiro.service.lgx;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.lgx.TCheckitemMapper;
import com.hu.zhcc.shiro.entity.lgx.TCheckitem;
import com.hu.zhcc.shiro.entity.xy.dto.TBodyTypeDTO;
import com.hu.zhcc.shiro.entity.xy.dto.TCheckitemDTO;
import com.hu.zhcc.shiro.entity.xy.pojo.TBodyType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.stream.events.DTD;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TCheckitemServiceImpl implements TCheckitemService {
    @Autowired
    TCheckitemMapper tCheckitemMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return tCheckitemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TCheckitem record) {
        return tCheckitemMapper.insert(record);
    }



    @Override
    @Transactional
    public TCheckitemDTO insertSelective(TCheckitemDTO record) {
        TCheckitem checkitem = new TCheckitem();
        BeanUtils.copyProperties(record,checkitem);
        tCheckitemMapper.insert(checkitem);
        record.setId(checkitem.getId());
        return record;
    }

    @Override
    public TCheckitem selectByPrimaryKey(Integer id) {
        return tCheckitemMapper.selectByPrimaryKey(id);
    }

    @Override
    public TCheckitemDTO selectById(Integer id) {
        TCheckitem checkitem = this.tCheckitemMapper.selectByPrimaryKey(id);
        if (checkitem == null){
            return null;
        }
        TCheckitemDTO dto = new TCheckitemDTO();

        BeanUtils.copyProperties(checkitem,dto);

        return dto;
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(TCheckitemDTO record) {
        TCheckitem checkitem = new TCheckitem();
        BeanUtils.copyProperties(record, checkitem);
        return tCheckitemMapper.updateByPrimaryKeySelective(checkitem);
    }

    @Override
    public int updateByPrimaryKey(TCheckitem record) {
        return tCheckitemMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<TCheckitem> tcheckitemList() {
        return tCheckitemMapper.tcheckitemList();
    }

    @Override
    public List<TCheckitem> tcheckitemId(int id) {
        return tCheckitemMapper.tcheckitemId(id);
    }

    @Override
    public Page<TCheckitemDTO> listCheckItem(Map<String, Object> parameters, int offset, int limit) {
        int total = this.tCheckitemMapper.countCheckItem(parameters);
        List<TCheckitemDTO> dtoList = null;
        if (total>0){
            PageRequest pageRequest = new PageRequest(offset, limit, parameters, null);
            List<TCheckitem> list = tCheckitemMapper.listCheckItem(pageRequest);
            dtoList = new ArrayList<>(list.size());
            for (TCheckitem item: list
                 ) {
                TCheckitemDTO dto = new TCheckitemDTO();
                BeanUtils.copyProperties(item,dto);
                dtoList.add(dto);
            }
        }
        return new Page<TCheckitemDTO>(total , dtoList);
    }

    @Override
    public List<TCheckitemDTO> listAllCheckItem() {
        List<TCheckitem> list = this.tCheckitemMapper.listAllCheckItem();
        ArrayList<TCheckitemDTO> dtos = new ArrayList<>(list.size());
        for (TCheckitem item: list) {
            TCheckitemDTO dto = new TCheckitemDTO();
            BeanUtils.copyProperties(item , dto);
            dtos.add(dto);
        }
        return dtos;
    }
}
